
package com.emsp.website.safetyStudy.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.emsp.website.safetyStudy.dao.QuestionsDao;
import com.emsp.website.safetyStudy.domain.Questions;
import com.emsp.website.safetyStudy.service.QuestionsService;

@Service
@Transactional
public class QuestionsServiceImpl implements QuestionsService {
	

	@Resource
	private QuestionsDao questionsDao;
/**
	 * 下载文字题
	 */
	public InputStream exportText() {
		String fileName = "text_template.xls";
		HSSFWorkbook wb = new HSSFWorkbook();
		//工作表
		HSSFSheet sheet = wb.createSheet("sheet1");
		//行
		HSSFRow row = sheet.createRow(0);
		//单元格
		row.createCell(0).setCellValue("文字题题干");
		row.createCell(1).setCellValue("A选项");
		row.createCell(2).setCellValue("B选项");
		row.createCell(3).setCellValue("C选项");
		row.createCell(4).setCellValue("D选项");
		row.createCell(5).setCellValue("答案");
		row.createCell(6).setCellValue("说明：本模板适用于文字题题型上传模板，内容以此表头为例,此列不能添加内容");
		
		//格式化，设置单元格的样式
		sheet.setColumnWidth(0, 6000);
		sheet.setColumnWidth(1, 6000);
		sheet.setColumnWidth(2, 6000);
		sheet.setColumnWidth(3, 6000);
		sheet.setColumnWidth(4, 6000);
		sheet.setColumnWidth(5, 2000);
		sheet.setColumnWidth(6, 8000);
		HSSFCellStyle style = wb.createCellStyle();
		style.setWrapText(true);
		row.getCell(0).setCellStyle(style);
		row.getCell(1).setCellStyle(style);
		row.getCell(2).setCellStyle(style);
		row.getCell(3).setCellStyle(style);
		row.getCell(4).setCellStyle(style);
		row.getCell(5).setCellStyle(style);
		//设置字体为红色
		style = wb.createCellStyle();
		style.setWrapText(true);
		HSSFFont font = wb.createFont();
		font.setColor(HSSFColor.RED.index);
		style.setFont(font);
		row.getCell(6).setCellStyle(style);
		
		File file = new File(fileName);
		try {
			OutputStream out=new FileOutputStream(file);
			wb.write(out);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		InputStream in=null;
		return in;
	}
}